home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / awt / event~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  11.0 KB  |  489 lines

  1. /*
  2.  * @(#)Event.java    1.32 95/12/14 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.io.*;
  22.  
  23. /**
  24.  * Event is a platform-independent class that encapsulates events from
  25.  * the local Graphical User Interface(GUI) platform.
  26.  *
  27.  * @version 1.32 12/14/95
  28.  * @author Sami Shaio
  29.  */
  30. public class Event {
  31.     private int data;
  32.  
  33.     /* Modifier constants */
  34.  
  35.     /**
  36.      * The shift modifier constant.
  37.      */
  38.     public static final int SHIFT_MASK         = 1 << 0;
  39.  
  40.     /**
  41.      * The control modifier constant.
  42.      */
  43.     public static final int CTRL_MASK         = 1 << 1;
  44.  
  45.     /** 
  46.      * The meta modifier constant.
  47.      */
  48.     public static final int META_MASK         = 1 << 2;
  49.  
  50.     /** 
  51.      * The alt modifier constant.
  52.      */
  53.     public static final int ALT_MASK         = 1 << 3;
  54.  
  55.     /* Action keys */
  56.     /** 
  57.      * The home key.
  58.      */
  59.     public static final int HOME         = 1000;
  60.  
  61.     /** 
  62.      * The end key. 
  63.      */
  64.     public static final int END         = 1001;
  65.  
  66.     /**
  67.      * The page up key.
  68.      */
  69.     public static final int PGUP         = 1002;
  70.  
  71.     /**
  72.      * The page down key.
  73.      */
  74.     public static final int PGDN         = 1003;
  75.  
  76.     /**
  77.      * The up arrow key.
  78.      */
  79.     public static final int UP             = 1004;
  80.  
  81.     /**
  82.      * The down arrow key.
  83.      */
  84.     public static final int DOWN         = 1005;
  85.  
  86.     /**
  87.      * The left arrow key.
  88.      */
  89.     public static final int LEFT         = 1006;
  90.  
  91.     /**
  92.      * The right arrow key.
  93.      */
  94.     public static final int RIGHT         = 1007;
  95.  
  96.     /**
  97.      * The F1 function key.
  98.      */
  99.     public static final int F1            = 1008;
  100.  
  101.     /**
  102.      * The F2 function key.
  103.      */
  104.     public static final int F2            = 1009;
  105.  
  106.     /**
  107.      * The F3 function key.
  108.      */
  109.     public static final int F3            = 1010;
  110.  
  111.     /**
  112.      * The F4 function key.
  113.      */
  114.     public static final int F4            = 1011;
  115.  
  116.     /**
  117.      * The F5 function key.
  118.      */
  119.     public static final int F5            = 1012;
  120.  
  121.     /**
  122.      * The F6 function key.
  123.      */
  124.     public static final int F6            = 1013;
  125.  
  126.     /**
  127.      * The F7 function key.
  128.      */
  129.     public static final int F7            = 1014;
  130.  
  131.     /**
  132.      * The F8 function key.
  133.      */
  134.     public static final int F8            = 1015;
  135.  
  136.     /**
  137.      * The F9 function key.
  138.      */
  139.     public static final int F9            = 1016;
  140.  
  141.     /**
  142.      * The F10 function key.
  143.      */
  144.     public static final int F10            = 1017;
  145.  
  146.     /**
  147.      * The F11 function key.
  148.      */
  149.     public static final int F11            = 1018;
  150.  
  151.     /**
  152.      * The F12 function key.
  153.      */
  154.     public static final int F12            = 1019;
  155.  
  156.  
  157.     /* Base for all window events. */
  158.     private static final int WINDOW_EVENT     = 200;
  159.  
  160.     /**
  161.      * The destroy window event.
  162.      */
  163.     public static final int WINDOW_DESTROY     = 1 + WINDOW_EVENT;
  164.  
  165.     /**
  166.      * The expose window event. 
  167.      */
  168.     public static final int WINDOW_EXPOSE     = 2 + WINDOW_EVENT;
  169.  
  170.     /** 
  171.      * The iconify window event. 
  172.      */
  173.     public static final int WINDOW_ICONIFY    = 3 + WINDOW_EVENT;
  174.  
  175.     /** 
  176.      * The de-iconify window event.
  177.      */
  178.     public static final int WINDOW_DEICONIFY    = 4 + WINDOW_EVENT;
  179.  
  180.     /**
  181.      * The move window event.
  182.      */
  183.     public static final int WINDOW_MOVED    = 5 + WINDOW_EVENT;
  184.  
  185.     /* Base for all keyboard events. */
  186.     private static final int KEY_EVENT         = 400;
  187.  
  188.     /**
  189.      * The key press keyboard event.
  190.      */
  191.     public static final int KEY_PRESS         = 1 + KEY_EVENT;
  192.  
  193.     /**
  194.      * The key release keyboard event.
  195.      */
  196.     public static final int KEY_RELEASE     = 2 + KEY_EVENT;
  197.  
  198.     /** 
  199.      * The key action keyboard event. 
  200.      */
  201.     public static final int KEY_ACTION         = 3 + KEY_EVENT;
  202.  
  203.     /** 
  204.      * The key action keyboard event. 
  205.      */
  206.     public static final int KEY_ACTION_RELEASE    = 4 + KEY_EVENT;
  207.  
  208.     /* Base for all mouse events. */
  209.     private static final int MOUSE_EVENT     = 500;
  210.  
  211.     /**
  212.      * The mouse down event.
  213.      */
  214.     public static final int MOUSE_DOWN         = 1 + MOUSE_EVENT;
  215.  
  216.     /**
  217.      *  The mouse up event.
  218.      */
  219.     public static final int MOUSE_UP         = 2 + MOUSE_EVENT;
  220.  
  221.     /**
  222.      * The mouse move event.
  223.      */
  224.     public static final int MOUSE_MOVE         = 3 + MOUSE_EVENT;
  225.  
  226.     /**
  227.      * The mouse enter event.
  228.      */
  229.     public static final int MOUSE_ENTER     = 4 + MOUSE_EVENT;
  230.  
  231.     /**
  232.      * The mouse exit event.
  233.      */
  234.     public static final int MOUSE_EXIT         = 5 + MOUSE_EVENT;
  235.  
  236.     /** 
  237.      * The mouse drag event.
  238.      */
  239.     public static final int MOUSE_DRAG         = 6 + MOUSE_EVENT;
  240.  
  241.  
  242.     /* Scrolling events */
  243.     private static final int SCROLL_EVENT     = 600;
  244.  
  245.     /** 
  246.      * The line up scroll event. 
  247.      */
  248.     public static final int SCROLL_LINE_UP    = 1 + SCROLL_EVENT;
  249.  
  250.     /**
  251.      * The line down scroll event.
  252.      */
  253.     public static final int SCROLL_LINE_DOWN    = 2 + SCROLL_EVENT;
  254.  
  255.     /**
  256.      * The page up scroll event.
  257.      */
  258.     public static final int SCROLL_PAGE_UP    = 3 + SCROLL_EVENT;
  259.  
  260.     /**
  261.      * The page down scroll event.
  262.      */
  263.     public static final int SCROLL_PAGE_DOWN    = 4 + SCROLL_EVENT;
  264.  
  265.     /**
  266.      * The absolute scroll event.
  267.      */
  268.     public static final int SCROLL_ABSOLUTE    = 5 + SCROLL_EVENT;
  269.     
  270.     /* List Events */
  271.     private static final int LIST_EVENT        = 700;
  272.  
  273.     /* Event sent when an item has been selected */
  274.     public static final int LIST_SELECT        = 1 + LIST_EVENT;
  275.  
  276.     /* Event sent when an item has been deselected */
  277.     public static final int LIST_DESELECT    = 2 + LIST_EVENT;
  278.  
  279.     /* Misc Event */
  280.     private static final int MISC_EVENT        = 1000;
  281.  
  282.     /**
  283.      * An action event.
  284.      */
  285.     public static final int ACTION_EVENT    = 1 + MISC_EVENT;
  286.  
  287.     /**
  288.      * A file loading event.
  289.      */
  290.     public static final int LOAD_FILE        = 2 + MISC_EVENT;
  291.  
  292.     /**
  293.      * A file saving event.
  294.      */
  295.     public static final int SAVE_FILE        = 3 + MISC_EVENT;
  296.  
  297.     /**
  298.      * A component gained the focus.
  299.      */
  300.     public static final int GOT_FOCUS        = 4 + MISC_EVENT;
  301.  
  302.     /**
  303.      * A component lost the focus.
  304.      */
  305.     public static final int LOST_FOCUS        = 5 + MISC_EVENT;
  306.     
  307.     /**
  308.      * The target component.
  309.      */
  310.     public Object target;
  311.  
  312.     /**
  313.      * The time stamp.
  314.      */
  315.     public long when;
  316.  
  317.     /**
  318.      * The type of this event. 
  319.      */
  320.     public int id;
  321.  
  322.     /** 
  323.      * The x coordinate of the event.
  324.      */
  325.     public int x;
  326.  
  327.     /** 
  328.      * The y coordinate of the event. 
  329.      */
  330.     public int y;
  331.  
  332.     /** 
  333.      * The key that was pressed in a keyboard event. 
  334.      */
  335.     public int key;
  336.  
  337.     /** 
  338.      * The state of the modifier keys.
  339.      */
  340.     public int modifiers;
  341.  
  342.     /**
  343.      * The number of consecutive clicks. This field is relevant only for
  344.      * MOUSE_DOWN events. If the field isn't set it will be 0. Otherwise,
  345.      * it will be 1 for single-clicks, 2 for double-clicks, and so on.
  346.      */
  347.     public int clickCount;
  348.  
  349.     /**
  350.      * An arbitraty argument.
  351.      */
  352.     public Object arg;
  353.  
  354.     /**
  355.      * The next event. Used when putting events into a linked list.
  356.      */
  357.     public Event evt;
  358.  
  359.     /**
  360.      * Constructs an event with the specified target component, time stamp,
  361.      * event type, x and y coordinates, keyboard key, state of the modifier
  362.      * keys and argument.
  363.      * @param target the target component
  364.      * @param when the time stamp
  365.      * @param id the event type
  366.      * @param x the x coordinate
  367.      * @param y the y coordinate
  368.      * @param key the key pressed in a keyboard event
  369.      * @param modifiers the state of the modifier keys
  370.      * @param arg the specified argument
  371.      */
  372.     public Event(Object target, long when, int id, int x, int y, int key,
  373.          int modifiers, Object arg) {
  374.     this.target = target;
  375.     this.when = when;
  376.     this.id = id;
  377.     this.x = x;
  378.     this.y = y;
  379.     this.key = key;
  380.     this.modifiers = modifiers;
  381.     this.arg = arg;
  382.     this.data = 0;
  383.     this.clickCount = 0;
  384.     }
  385.  
  386.     /**
  387.      * Constructs an event with the specified target component, time stamp,
  388.      * event type, x and y coordinates, keyboard key, state of the modifier
  389.      * keys and an argument set to null. 
  390.      * @param target the target component
  391.      * @param when the time stamp
  392.      * @param id the event type
  393.      * @param x the x coordinate
  394.      * @param y the y coordinate
  395.      * @param key the key pressed in a keyboard event
  396.      * @param modifiers the state of the modifier keys
  397.      */
  398.     public Event(Object target, long when, int id, int x, int y, int key, int modifiers) {
  399.     this(target, when, id, x, y, key, modifiers, null);
  400.     }
  401.  
  402.     /**
  403.      * Constructs an event with the specified target component, 
  404.      * event type, and argument. 
  405.      * @param target the target component
  406.      * @param id the event type
  407.      * @param arg the specified argument
  408.      */
  409.     public Event(Object target, int id, Object arg) {
  410.     this(target, 0, id, 0, 0, 0, 0, arg);
  411.     }
  412.  
  413.     /** 
  414.      * Translates an event relative to the given component. This
  415.      * involves at a minimum translating the coordinates so they make
  416.      * sense within the given component. It may also involve
  417.      * translating a region in the case of an expose event.
  418.      * @param x the x coordinate
  419.      * @param y the y coordinate
  420.      */
  421.     public void translate(int x, int y) {
  422.     this.x += x;
  423.     this.y += y;
  424.     }
  425.  
  426.     /**
  427.      * Checks if the shift key is down.
  428.      * @see #modifiers
  429.      * @see #controlDown
  430.      * @see #metaDown
  431.      */
  432.     public boolean shiftDown() {
  433.     return (modifiers & SHIFT_MASK) != 0;
  434.     }
  435.  
  436.     /**
  437.      * Checks if the control key is down.
  438.      * @see #modifiers
  439.      * @see #shiftDown
  440.      * @see #metaDown
  441.      */
  442.     public boolean controlDown() {
  443.     return (modifiers & CTRL_MASK) != 0;
  444.     }
  445.  
  446.     /**
  447.      * Checks if the meta key is down.
  448.      * @see #modifiers
  449.      * @see #shiftDown
  450.      * @see #controlDown
  451.      */
  452.     public boolean metaDown() {
  453.     return (modifiers & META_MASK) != 0;
  454.     }
  455.  
  456.     /**
  457.      * Returns the parameter String of this Event. 
  458.      */
  459.     protected String paramString() {
  460.     String str = "id=" + id + ",x=" + x + ",y=" + y;
  461.     if (key != 0) {
  462.         str += ",key=" + key;
  463.     }
  464.     if (shiftDown()) {
  465.         str += ",shift";
  466.     }
  467.     if (controlDown()) {
  468.         str += ",control";
  469.     }
  470.     if (metaDown()) {
  471.         str += ",meta";
  472.     }
  473.     if (target != null) {
  474.         str += ",target=" + target;
  475.     }
  476.     if (arg != null) {
  477.         str += ",arg=" + arg;
  478.     }
  479.     return str;
  480.     }
  481.  
  482.     /**
  483.      * Returns the String representation of this Event's values.
  484.      */
  485.     public String toString() {
  486.     return getClass().getName() + "[" + paramString() + "]";
  487.     }
  488. }
  489.